package com.leicx.guli.order.config;

import feign.RequestInterceptor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author daxiong
 * @date 2022/8/9 下午8:21
 */
@SpringBootConfiguration
public class GuliFeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            ServletRequestAttributes requestAttributes =
                    (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (Objects.nonNull(requestAttributes)) {
                HttpServletRequest request = requestAttributes.getRequest();
                // 获取老请求的cookie
                String cookie = request.getHeader("Cookie");
                // 给新的请求添加上cookie
                template.header("Cookie", cookie);
            }
        };
    }
}
